<?php
// 鍒嗙被绠＄悊
class CategoryInfoAction extends CommonAction {
	private static $nodeList = array();
	private $TreeFolderHTML  = '';

	public function _initialize() {
		parent::_initialize();
		$this->pk = 'category_id'; //设置主键
		$name     = $this->getActionName();
		$model    = M($name);
		if (!empty($_REQUEST['active'])) {
			self::$nodeList = $this->treeBuild($model->select(), false);
		} else {
			self::$nodeList = $this->treeBuild($model->select());
		}
		$this->prepareParm();
	}

	protected function prepareParm() {
		$this->assign('APP_CATEGORY', C('APP_CATEGORY'));
	}

	public function index() {
		if (!isset($_REQUEST['category_type'])) {

		} elseif (empty($_REQUEST['category_type'])) {
			$this->error('请选择一个分类！');
		} else {
			$category_type        = $_REQUEST['category_type'];
			$this->TreeFolderHTML = '';
			$this->createTreeFolderHTML(self::$nodeList[0], $category_type);
			$this->assign('TreeFolderHTML', $this->TreeFolderHTML);
		}
		$this->assign('category_type', $category_type);
		$this->display();
	}

	public function delete() {
		$name               = $this->getActionName();
		$model              = M($name);
		$map['category_id'] = $_REQUEST['categoryId'];
		$list               = array();
		$ids                = $this->searchChildren($list, self::$nodeList[$map['category_id']]);
		$condition          = array('category_id' => array('in', $ids));
		$list               = $model->where($map)->delete();
		if ($list !== false) {
			$this->success('删除成功！');
		} else {
			$this->error('删除失败！');
		}
	}

	public function searchChildren($list, $node) {
		$list[] = $node->self->categoryId;
		foreach ($node->children as $key => $id) {
			$list = $this->searchChildren($list, self::$nodeList[$id]);
		}
		return $list;
	}

	protected function treeBuild($data, $active = true) {
		$nodeList                     = array();
		$rootnode                     = new stdClass();
		$rootnode->parent             = null;
		$rootnode->children           = array();
		$rootnode->self->categoryId   = 0;
		$rootnode->self->categoryName = '根节点';
		$rootnode->self->level        = 0;
		$nodeList[]                   = $rootnode;
		foreach ($data as $key => $value) {
			if ($active && $value['status'] == 10) {
				continue;
			}
			if (!isset($nodeList[$value['category_id']])) {
				$nodeList[$value['category_id']] = new stdClass();
			}
			if (!isset($nodeList[$value['up_category_id']])) {
				self::$nodeList[$value['up_category_id']] = new stdClass();
			}
			if (!isset($nodeList[$value['up_category_id']]->children)) {
				$nodeList[$value['up_category_id']]->children = array();
			}
			$nodeList[$value['category_id']]->self->categoryId   = $value['category_id'];
			$nodeList[$value['category_id']]->self->categoryName = $value['category_name'];
			$nodeList[$value['category_id']]->self->level        = $value['level'];
			$nodeList[$value['category_id']]->self->categoryType = $value['category_type'];
			$nodeList[$value['category_id']]->parent             = $value['up_category_id'];
			$nodeList[$value['up_category_id']]->children[]      = $value['category_id'];
		}
		return $nodeList;
	}

	protected function createTreeFolderHTML($node, $category_type) {
		if ($node->self->categoryType != $category_type && $node->self->categoryId != 0) {
			return false;
		}
		if (!isset($node->self->categoryId) || !isset($node->self->categoryName)) {
			return false;
		}
		$this->TreeFolderHTML .= '<li target="sid_category_id" rel="' . $node->self->categoryId . '" class="folder"><a href="__APP__/CategoryInfo/detail/category_id/' . $node->self->categoryId . '" target="ajax" rel="jbsxBox" tname="level_' . $node->self->level . '" tvalue="' . $node->self->categoryId . '">' . $node->self->categoryName . '</a>';
		//$this->TreeFolderHTML .= '<li><a href="javascript:;" tname="level_' . $node->self->level . '" tvalue="' . $node->self->categoryId . '">' . $node->self->categoryName . '</a>';
		if (empty($node->children)) {
			return false;
		} else {
			$this->TreeFolderHTML .= '<ul>';
			foreach ($node->children as $k => $id) {
				$this->createTreeFolderHTML(self::$nodeList[$id], $category_type);
			}
			$this->TreeFolderHTML .= '</ul>';
		}
		$this->TreeFolderHTML .= '</li>';
		return true;
	}

	public function add() {
		$category_type = $_REQUEST['categoryType'];
		if (empty($category_type)) {
			$this->error('请选择一个分类类别！');
		}
		$this->assign('category_type', $category_type);
		$this->display();
	}

	public function detail() {
		$name               = $this->getActionName();
		$model              = M($name);
		$map['category_id'] = $_REQUEST['category_id'];
		$list               = $model->where($map)->select();
		$vo                 = $list[0];
		$this->assign('vo', $vo);
		$this->display();
	}

	public function insert() {
		$name                   = $this->getActionName();
		$model                  = M($name);
		$res                    = $model->field('max(category_id) as maxId')->select();
		$data['category_id']    = $res[0]['maxId'] + 1;
		$map['category_id']     = $_REQUEST['up_category_id'];
		$res                    = $model->where($map)->select();
		$data['level']          = $res[0]['level'] + 1;
		$data['category_type']  = $_REQUEST['category_type'];
		$data['category_name']  = $_REQUEST['category_name'];
		$data['orderby']        = $_REQUEST['orderby'];
		$data['up_category_id'] = $_REQUEST['up_category_id'];
		$data['status']         = $_REQUEST['status'];
		$data['remark']         = $_REQUEST['remark'];
		//计算level
		$model->data($data)->add();
		$this->success('添加成功！');
	}

	public function edit() {
		$category_id                = $_REQUEST['categoryId'];
		$name                       = $this->getActionName();
		$model                      = M($name);
		$map['category_id']         = $category_id;
		$res                        = $model->where($map)->select();
		$res[0]['up_category_name'] = self::$nodeList[$res[0]['up_category_id']]->self->categoryName;
		$this->assign('vo', $res[0]);
		$this->display();
	}

	public function update() {
		$name                   = $this->getActionName();
		$model                  = M($name);
		$res                    = $model->field('max(category_id) as maxId')->select();
		$where['category_id']   = $_REQUEST['category_id'];
		$data['up_category_id'] = $_REQUEST['up_category_id'];
		$data['category_name']  = $_REQUEST['category_name'];
		$data['orderby']        = $_REQUEST['orderby'];
		$data['remark']         = $_REQUEST['remark'];
		$data['status']         = $_REQUEST['status'];
		//计算level
		$model->where($where)->save($data);
		$this->success('添加成功！');
	}
}

?>